package org.budo.dubbo.protocol.http.view.render.budo;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @author lmw
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ApiError implements Serializable{
    private static final long serialVersionUID = 1673074431281406827L;

    // 10 系统级
    public static final ApiError SERVER_ERROR = new ApiError(1001, "server_error", "服务器异常");

    public static final ApiError INTERFACE_MISS_MATCH = new ApiError(1002, "interface_miss_match", "没有这个接口");

    public static final ApiError METHOD_MISS_MATCH = new ApiError(1003, "method_miss_match", "接口没有这个方法");

    public static final ApiError RPC_ERROR = new ApiError(1004, "rpc_error", "服务调用异常");

    public static final ApiError NO_PROVIDER_ERROR = new ApiError(1005, "no_provider_error", "no provider for service");

    // 11 授权 token
    public static final ApiError AUTHENTICATION_FAILED = new ApiError(1101, "authentication_failed", "授权验证失败");

    public static final ApiError TOKEN_EMPTY = new ApiError(1102, "token_empty", "需要传递参数 _token");

    public static final ApiError TOKEN_NOT_FOUND = new ApiError(1103, "token_not_found", "传入的token无效");

    public static final ApiError TOKEN_EXPIRED = new ApiError(1104, "token_expired", "传入的token已过期");

    // 12 sign
    public static final ApiError SIGN_CHECK_FAILED = new ApiError(1201, "sign_check_failed", "签名验证失败");

    public static final ApiError APP_KEY_EMPTY = new ApiError(1202, "app_key_empty", "需要传递参数 _app_key");

    public static final ApiError TIME_EMPTY = new ApiError(1203, "time_empty", "需要传递参数 _time");

    public static final ApiError APP_SECRET_EMPTY = new ApiError(1204, "app_secret_empty", "服务器端配置的当前appKey对应的appSecret为空");

    public static final ApiError SIGN_ERROR = new ApiError(1205, "sign_error", "签名验证不匹配");

    // 13 用户权限
    public static final ApiError USER_PERMISSION_ERROR = new ApiError(1301, "user_permission_error", "用户没有这个权限");

    public static final ApiError APP_PERMISSION_ERROR = new ApiError(1302, "app_permission_error", "当前App没有这个权限");

    // 14 账户校验
    public static final ApiError AUTHORIZE_FAIL = new ApiError(1401, "authorize_fail", "用户认证失败");

    public static final ApiError USER_NOT_FOUND = new ApiError(1402, "user_not_found", "用户未找到");

    public static final ApiError PASSWORD_INCORRECT = new ApiError(1403, "password_incorrect", "密码不正确");

    public static final ApiError USERNAME_OR_PASSWORD_INCORRECT = new ApiError(1404, "USERNAME_OR_PASSWORD_INCORRECT", "用户名或密码不正确");

    public static final ApiError USER_DISABLED = new ApiError(1405, "user_disabled", "用户被冻结");

    // 12 参数
    public static final ApiError ILLEGAL_PARAMETER = new ApiError(2001, "illegal_parameter", "非法参数");

    public static final ApiError MISS_REQUIRED_PARAMETER = new ApiError(2002, "miss_required_parameter", "缺少必选参数");

    public static final ApiError PARAMETER_TYPE_MISMATCH = new ApiError(2003, "parameter_type_mismatch", "参数类型不匹配");

    public static final ApiError PARAMETER_LENGTH_EXCEED = new ApiError(2004, "parameter_length_exceed", "参数长度不符合限制");

    public static final ApiError PARAMETER_SIZE_EXCEED = new ApiError(2004, "parameter_size_exceed", "参数大小不符合限制");

    public static final ApiError PARAMETER_TOO_SMALL = new ApiError(2004, "parameter_too_small", "数字参数太小");

    public static final ApiError PARAMETER_TOO_BIG = new ApiError(2005, "parameter_too_big", "数字参数太大");

    public static final ApiError PARAMETER_PATTERN_MISMATCH = new ApiError(2006, "parameter_pattern_mismatch", "参数不符合正则表达式");

    public static final ApiError NOT_SUPPORTED_TIME_FORMAT = new ApiError(2007, "not_supported_time_format", "不支持的时间格式");

    public static final ApiError ILLEGAL_ENUM_PARAMETER = new ApiError(2008, "illegal_enum_parameter", "枚举参数错误");

    public static final ApiError ILLEGAL_ARRAY_PARAMETER = new ApiError(2009, "illegal_array_parameter", "数组参数错误");

    // 13 Assert
    public static final ApiError ASSERT_FAILED = new ApiError(3001, "assert_failed", "某些状态或值不符合预期");

    public static final ApiError ASSERT_NOT_NULL_FAILED = new ApiError(3002, "assert_not_null_failed", "有一个不应该为空的值为空");

    public static final ApiError ASSERT_STRING_NOT_EMPTY_FAILED = new ApiError(3003, "assert_string_not_empty_failed", "有一个不应该为空的字符串为空");

    public static final ApiError ASSERT_COLLECTION_NOT_EMPTY_FAILED = new ApiError(3004, "assert_collection_not_empty_failed", "有一个不应该为空的集合为空");

    public static final ApiError ASSERT_MAP_NOT_EMPTY_FAILED = new ApiError(3005, "assert_map_not_empty_failed", "有一个不应该为空的Map为空");

    public static final ApiError ASSERT_ARRAY_NOT_EMPTY_FAILED = new ApiError(3006, "assert_array_not_empty_failed", "有一个不应该为空的数组为空");

    // 14 BUSINESS_ERROR
    public static final ApiError BUSINESS_ERROR = new ApiError(4001, "business_error", "业务异常");

    private Integer code;

    private String name;

    private String description;

    public ApiError withDescription(String description) {
        if (null == description || description.trim().isEmpty()) {
            description = this.getDescription();
        }

        return new ApiError(this.getCode(), this.getName(), description);
    }

    public String toString() {
        return super.toString() + ", code=" + this.getCode() + ", name=" + this.getName() + ", description=" + this.getDescription();
    }
}
